package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.model.system.SysRole;
import com.open.capacity.user.dao.SysPostDao;
import com.open.capacity.user.model.SysCompany;
import com.open.capacity.user.model.SysPost;
import com.open.capacity.user.service.SysPostService;
import com.open.capacity.user.vo.SysCompanyExcel;
import com.open.capacity.user.vo.SysPostExcel;
import com.open.capacity.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class SysPostServiceImpl implements SysPostService {

    @Autowired
    private SysPostDao postDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysPost sysPost) {
        SysPost post = postDao.findByCode(sysPost.getCompanyId(), sysPost.getDepartmentId(), sysPost.getCode());
        if (post != null) {
            throw new IllegalArgumentException("岗位code已存在");
        }
        sysPost.setCreateTime(new Date());
        sysPost.setUpdateTime(sysPost.getCreateTime());

        postDao.save(sysPost);
        log.info("保存岗位：{}", sysPost);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysPost sysPost) {
        sysPost.setUpdateTime(new Date());

        postDao.updateByOps(sysPost);
        log.info("修改岗位：{}", sysPost);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SysPost sysPost = postDao.findById(id);

        postDao.delete(id);

        log.info("删除岗位：{}", sysPost);
    }


    @Override
    public SysPost findById(String id) {
        return postDao.findById(id);
    }

    @Override
    public List<SysPost> findPosts(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<SysPost> list = postDao.findList(params);

        return list;
    }

    @Override
    public List<SysPost> findByParentId(String parentId) {
        return postDao.findByParentId(parentId);
    }


    @Override
    public Result saveOrUpdate(SysPost sysPost, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            SysPost model = postDao.findById(keyValue);
            if (model != null) {
                sysPost.setId(keyValue);
                sysPost.setUpdateTime(new Date());
                i = postDao.updateByOps(sysPost);
            }
        } else {
            SysPost post = postDao.findByCode(sysPost.getCompanyId(), sysPost.getDepartmentId(), sysPost.getCode());
            if (post != null) {
                throw new IllegalArgumentException("岗位code已存在");
            }
            sysPost.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            sysPost.setCreateTime(new Date());
            sysPost.setUpdateTime(sysPost.getCreateTime());
            i = postDao.save(sysPost);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


    @Override
    public List<SysPostExcel> findExcels(Map<String, Object> params) {
        List<SysPost> list = postDao.findList(params);
        List<SysPostExcel> result = new ArrayList<>();
        for (SysPost post : list) {
            SysPostExcel excel = new SysPostExcel();
            BeanUtils.copyProperties(post, excel);
            result.add(excel);
        }
        return result;
    }
}
